package job._20240305tencent;

import java.util.Arrays;

/**
 * @Project : algorithm-learning-java
 * @Author : Ruoyu Wang
 * @User : Momenta
 * @DateTime : 2024/3/5 16:11
 */

// leetcode 189
public class Q1 {
    // O(n) O(n)
//    public static void main(String[] args) {
//        int[] arr = {1,2,3,4,5,6,7};
//        int k = 5;
//
//        int[] tmp = new int[k];
//        for (int i = arr.length-1,j=k-1; j>=0; j--,i--) {
//            tmp[j] = arr[i];
//        }
//
//        for (int i = arr.length-1,j=arr.length-1-k; j>=0 ; i--,j--) {
//            arr[i] = arr[j];
//        }
//
//        for (int i = 0; i < k; i++) {
//            arr[i] = tmp[i];
//        }
//
//        System.out.println(Arrays.toString(arr));
//    }

    // O(n) O(1)
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        int k = 7;

        k = k % arr.length;

        change(arr, arr.length - k, arr.length - 1);
        change(arr, 0, arr.length - 1);
        change(arr, arr.length - (arr.length - k), arr.length - 1);

        System.out.println(Arrays.toString(arr));
    }

    private static void change(int[] nums, int l, int r) {
        int tmp = 0;
        for (int i = l, j = r; i < j; i++, j--) {
            tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }
    }
}